﻿// <copyright file="CharacterAddEditGM.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System;
    using System.Collections.Generic;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Presentation;
    using Microsoft.LightSwitch.Presentation.Extensions;
    using System.Windows.Controls;
    using System.IO;

    public partial class CharacterAddEditGM
    {
        partial void CharacterAddEditGM_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.CharacterId == null)
            {
                this.Character = new Character();
                this.Character.State = (int)CharacterStateEnum.Approved;
                this.Character.Visibility = (int)VisibilityEnum.Public;
                this.DisplayName = "Add new Character";
            }
            else
            {
                this.Character = this.CharacterQuery;
                this.DisplayName = string.Format("Edit {0}", this.Character.Name);
            }
        }

        #region Approval/Reject and Notifications

        partial void ApproveCharacter_CanExecute(ref bool result)
        {
            result = this.Character.State == (int)CharacterStateEnum.Submitted;
        }

        partial void ApproveCharacter_Execute()
        {
            this.DataWorkspace.ApplicationData.SaveChanges();

            this.Character.State = (int)CharacterStateEnum.Approved;
            this.DataWorkspace.ApplicationData.SaveChanges();

            // Create an automatic notification for this for the player
            if (this.Character.Owner != null)
            {
                var notification = this.Notifications.AddNew();
                notification.Read = false;
                notification.SentDate = DateTime.Now;
                notification.Recipient = this.Character.Owner;
                notification.Title = "Character Approved";
                notification.Message = string.Format("{0} has been approved.", this.Character.Name);

                this.FindControl("ModalNotification").DisplayName = "Send Notification";
                this.OpenModalWindow("ModalNotification");
            }
        }

        partial void RejectCharacter_CanExecute(ref bool result)
        {
            result = this.Character.State == (int)CharacterStateEnum.Submitted;
        }

        partial void RejectCharacter_Execute()
        {
            this.DataWorkspace.ApplicationData.SaveChanges();

            this.Character.State = (int)CharacterStateEnum.Rejected;
            this.DataWorkspace.ApplicationData.SaveChanges();

            // Create an automatic notification for this for the player
            if (this.Character.Owner != null)
            {
                var notification = this.Notifications.AddNew();
                notification.Read = false;
                notification.Recipient = this.Character.Owner;
                notification.Title = "Character Rejected";
                notification.Message = string.Format("{0} has been rejected.", this.Character.Name);

                this.FindControl("ModalNotification").DisplayName = "Send Notification";
                this.OpenModalWindow("ModalNotification");
            }
        }

        partial void SendNotification_CanExecute(ref bool result)
        {
            result = this.Character.Owner != null;
        }

        partial void SendNotification_Execute()
        {
            // Create the notification
            var notification = this.Notifications.AddNew();
            notification.Read = false;
            notification.SentDate = DateTime.Now;
            notification.Recipient = this.Character.Owner;

            this.FindControl("ModalNotification").DisplayName = "Send Notification";
            this.OpenModalWindow("ModalNotification");
        }

        partial void SubmitModalNotification_Execute()
        {
            this.CloseModalWindow("ModalNotification");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalNotification_Execute()
        {
            this.CloseModalWindow("ModalNotification");
            this.Notifications.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Character Sheet

        partial void UploadCharacterSheet_Execute()
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                {
                    OpenFileDialog dialog = new OpenFileDialog();
                    if (dialog.ShowDialog() == true)
                    {
                        this.Character.CharacterSheetFilename = dialog.File.Name;
                        using (FileStream stream = dialog.File.OpenRead())
                        {
                            byte[] buffer = new byte[dialog.File.Length];
                            stream.Read(buffer, 0, (int)dialog.File.Length);
                            this.Character.CharacterSheetData = buffer;
                        }
                    }
                });
        }

        partial void DownloadCharacterSheet_CanExecute(ref bool result)
        {
            result = this.Character.CharacterSheetData != null;
        }

        partial void DownloadCharacterSheet_Execute()
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                {
                    SaveFileDialog dialog = new SaveFileDialog();
                    dialog.DefaultExt = Path.GetExtension(this.Character.CharacterSheetFilename);
                    dialog.Filter = string.Format("Character sheet (*{0})|*{0}", Path.GetExtension(this.Character.CharacterSheetFilename));
                    if (dialog.ShowDialog() == true)
                    {
                        using (Stream file = dialog.OpenFile())
                        {
                            file.Write(this.Character.CharacterSheetData, 0, this.Character.CharacterSheetData.Length);
                        }
                    }
                });
        }

        #endregion

        #region Add Blood Ability

        partial void RelationCharacterBloodAbilitiesAddAndEditNew_Execute()
        {
            this.RelationCharacterBloodAbilities.AddNew();

            this.FindControl("ModalWindowBloodAbility").DisplayName = "Add new Blood Ability";
            this.OpenModalWindow("ModalWindowBloodAbility");
        }

        partial void SubmitModalWindowBloodAbility_Execute()
        {
            this.CloseModalWindow("ModalWindowBloodAbility");
        }

        partial void CancelModalWindowBloodAbility_Execute()
        {
            this.CloseModalWindow("ModalWindowBloodAbility");
            this.RelationCharacterBloodAbilities.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add CharacterClass

        partial void RelationCharacterClassesAddAndEditNew_Execute()
        {
            this.RelationCharacterClasses.AddNew();

            this.FindControl("ModalClasses").DisplayName = "Add new Class";
            this.OpenModalWindow("ModalClasses");
        }

        partial void SubmitModalWindowClasses_Execute()
        {
            this.CloseModalWindow("ModalClasses");
        }

        partial void CancelModalWindowClasses_Execute()
        {
            this.CloseModalWindow("ModalClasses");
            this.RelationCharacterClasses.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add Skill

        partial void RelationCharacterSkillsAddAndEditNew_Execute()
        {
            this.RelationCharacterSkills.AddNew();

            this.FindControl("ModalSkills").DisplayName = "Add new Skill";
            this.OpenModalWindow("ModalSkills");
        }

        partial void SubmitModalWindowSkills_Execute()
        {
            this.CloseModalWindow("ModalSkills");
        }

        partial void CancelModalWindowSkills_Execute()
        {
            this.CloseModalWindow("ModalSkills");
            this.RelationCharacterSkills.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add Existing/Edit Domain

        partial void DomainsAddExisting_Execute()
        {
            this.FindControl("ModalDomains").DisplayName = "Add existing Domain";
            this.OpenModalWindow("ModalDomains");
        }

        partial void DomainsEditSelected_CanExecute(ref bool result)
        {
            result = this.Domains.SelectedItem != null;
        }

        partial void DomainsEditSelected_Execute()
        {
            this.Application.ShowDomainAddEditGM(this.Domains.SelectedItem.Id);
        }

        partial void SubmitModalDomains_Execute()
        {
            this.CloseModalWindow("ModalDomains");
            this.Character.Domains.Add(this.DomainToAddToCharacter);
            this.DataWorkspace.ApplicationData.SaveChanges();

            // Refresh the involved queries
            this.Domains.Refresh();
        }

        partial void CancelModalDomains_Execute()
        {
            this.CloseModalWindow("ModalDomains");
        }

        #endregion

        #region Add Existing/Edit Lieutenant

        partial void LieutenantsAddAndEditNew_Execute()
        {
            this.FindControl("ModalLieutenants").DisplayName = "Add existing Lieutenant";
            this.OpenModalWindow("ModalLieutenants");
        }

        partial void LieutenantsEditSelected_CanExecute(ref bool result)
        {
            result = this.Lieutenants.SelectedItem != null;
        }

        partial void LieutenantsEditSelected_Execute()
        {
            this.Application.ShowCharacterAddEditGM(this.Lieutenants.SelectedItem.Id);
        }

        partial void SubmitModalLieutenants_Execute()
        {
            this.CloseModalWindow("ModalLieutenants");
            this.Character.Lieutenants.Add(this.LieutenantToAddToCharacter);
            this.DataWorkspace.ApplicationData.SaveChanges();

            // Refresh the involved queries
            this.Lieutenants.Refresh();
        }

        partial void CancelModalLieutenants_Execute()
        {
            this.CloseModalWindow("ModalLieutenants");
        }

        #endregion
    }
}